﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AMicroblogAPI;
using AMicroblogAPI.Common;
using AMicroblogAPI.DataContract;

namespace weibo
{
    public partial class DetailStatus : Form
    {
        private long statusID;
        public DetailStatus(long id)
        {
            InitializeComponent();
            statusID = id;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnComment_Click(object sender, EventArgs e)
        {
            CommentA ca = new CommentA(statusID);
            int ori = AMicroblog.GetComments(statusID).Items.Count;
            ca.ShowDialog();
            if (ori != AMicroblog.GetComments(statusID).Items.Count)
            {
                initialComment();
            }

        }

        private void DetailStatus_Load(object sender, EventArgs e)
        {
            StatusInfo si = AMicroblog.GetStatus(statusID);
            tbCon.Text = si.Text;
            lbStatusName.Text = si.User.ScreenName;
            lbtime.Text = si.CreatedAt;
            if (AMicroblog.GetStatus(statusID).User.ID.ToString() == Login.accessToken.UserID)
            {
                btnDelete.Enabled = true;
            }
            else
            {
                btnDelete.Enabled = false;
            }
            initialComment();
        }

        private void initialComment()
        {
            lvComment.Items.Clear();
            lvComment.GridLines = true;
            lvComment.View = View.Details;
            lvComment.LabelEdit = false;
            lvComment.Scrollable = true;
            lvComment.FullRowSelect = false;
            lvComment.Columns.Add("CommentID");
            lvComment.Columns.Add("ScreenName");
            lvComment.Columns.Add("CreateAt");
            lvComment.Columns.Add("Text");

            List<CommentInfo> comments = new List<CommentInfo>();
            comments.AddRange(AMicroblog.GetComments(statusID).Items);
            foreach (CommentInfo ci in comments)
            {
                ListViewItem lvi = new ListViewItem(new string[] { ci.ID.ToString(), ci.User.ScreenName, ci.CreatedAt, ci.Text });
                lvComment.Items.Add(lvi);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确定要删除吗？", "...", MessageBoxButtons.OKCancel);
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                AMicroblog.DeleteStatus(statusID);
                this.Close();
                
            }
        }


    }
}
